package com.lzx.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzx.api.CommonResult;
import com.lzx.dto.CategoryDto;
import com.lzx.entity.Category;
import com.lzx.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (Category)表控制层
 *
 * @author makejava
 * @since 2021-03-04 19:32:20
 */
@RestController
@RequestMapping("category")
public class CategoryController {
    /**
     * 服务对象
     */
    @Resource
    private CategoryService categoryService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Category selectOne(Integer id) {
        return this.categoryService.queryById(id);
    }

    /**
     * 获取所有分类
     * @return
     */
    @GetMapping("/getAll")
    public CommonResult getAll(){
        List<Category> categoryList = categoryService.list();
        return CommonResult.success(categoryList);
    }

    /**
     * 获取分类列表
     * @return 返回级联数据格式
     */
    @RequestMapping("/getByCase")
    public CommonResult<List<Tree<String>>> getByCase(){
        List<Category> categoryList = categoryService.list();
        List<TreeNode<Integer>> collect = categoryList.stream().map(category -> {
            Map<String, Object> map = new HashMap<>();
            map.put("level", category.getLevel());
            map.put("Pid", category.getParentid());
            map.put("description", category.getSeodescription());
            TreeNode<Integer> treeNode = new TreeNode<Integer>().setId(category.getId())
                    .setName(category.getName())
                    .setParentId(category.getParentid())
                    .setExtra(map);
            return treeNode;
        }).collect(Collectors.toList());

        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();

        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setChildrenKey("children");
        // 最大递归深度
        treeNodeConfig.setDeep(9);

        // 构建node列表
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        //转换器
        List<Tree<String>> treeNodes = TreeUtil.build(collect, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                    tree.setName(treeNode.getName());
                    tree.putExtra("level", treeNode.getExtra().getOrDefault("level", null));
                    tree.putExtra("description", treeNode.getExtra().getOrDefault("description", null));
                });
        return CommonResult.success(treeNodes);
    }
    @GetMapping("/getByBrand")
    public CommonResult getByBrand(@RequestParam  Integer brandId){
        List<Category> categoryList = categoryService.getByBrandId(brandId);
        if(BeanUtil.isNotEmpty(categoryList)){
            return CommonResult.success(categoryList);
        }
        return CommonResult.failed();
    }
    @PostMapping("/addOrEdit")
    public CommonResult addCategory(@RequestBody CategoryDto categoryDto){
        Category category = new Category();
        if(categoryDto.getParent()==null){
            categoryDto.setParent(0);
        }
        category.setId(categoryDto.getId());
        category.setParentid(categoryDto.getParent());
        category.setSeodescription(categoryDto.getDescrible());
        category.setName(categoryDto.getName());
        if (category.getParentid() == 0) {
            category.setLevel(1);
        } else {
            category.setLevel(categoryService.getById(category.getParentid()).getLevel() + 1);
        }
        categoryService.saveOrUpdate(category);
        return CommonResult.success("");
    }
    @PostMapping("/remove")
    public CommonResult remove(Integer id){
        if(BeanUtil.isEmpty(id)){
            return CommonResult.failed();
        }
        categoryService.deleteById(id);
        return CommonResult.success("删除成功");
    }

}
